package com.example.android.robotium.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android.robotium.R;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        RecyclerView rv = (RecyclerView) findViewById(R.id.ryc);
        ListView lv = (ListView) findViewById(R.id.lv);
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        MyAdapter mAdapter = new MyAdapter();
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.setAdapter(mAdapter);
        rv.setAdapter(new MyAdapter());

        lv.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return 200;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
                View view = layoutInflater.inflate(R.layout.ry_itme_layout, parent, false);
                ((TextView) view.findViewById(R.id.tv)).setText("lsview==================" + position);

                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,"lsview===="+position,Toast.LENGTH_LONG).show();
                        gotoActivity();
                    }
                });

                return view;

            }
        });
    }


    class MyViewHoder extends RecyclerView.ViewHolder {
        TextView tv;
        View itemView;

        public MyViewHoder(View itemView) {
            super(itemView);
            itemView = itemView;
            tv = itemView.findViewById(R.id.tv);

        }
    }


    class MyAdapter extends RecyclerView.Adapter<MyViewHoder> {

        @Override
        public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
            View view = layoutInflater.inflate(R.layout.ry_itme_layout, parent, false);

            return new MyViewHoder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHoder holder, final int position) {
            holder.tv.setText("ryview==================" + position);
            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this,"ryview===="+position,Toast.LENGTH_LONG).show();
                    gotoActivity();
                }
            });
        }

        @Override
        public int getItemCount() {
            return 200;
        }
    }


   public void gotoActivity(){
       Intent  intent = new Intent(this,ContentActivity.class);
       startActivity(intent);
    }

}
